package cannot.backcontroller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cannot.pojo.Remark;
import cannot.pojo.Tv;
import cannot.service.TvService;
/**
 * 分类页控制器
 * @author Administrator
 *
 */
@Controller
public class TvCategoryController {
	
	@Autowired
	TvService tvService;
	
	
	@RequestMapping("loadTvCategoryPage")
	public String loadTvCategoryPage(Integer mainTypeId,Integer otherTypeId,Model m){
		m.addAttribute("mainTypeId", mainTypeId);
		m.addAttribute("otherTypeId", otherTypeId);
		return "include/category/category";
	}
	
	
	@RequestMapping("loadTvCategory")
	@ResponseBody
	public Object loadTvCategory(Integer mainTypeId,@RequestParam(value="pagenumber",defaultValue="0")Integer pagenumber,Integer otherTypeId,String sort){
		PageHelper.startPage(pagenumber,24);
		String sortStr = typeSort(sort);
		List<Tv> tvs = new ArrayList<>();
		
		//如果类别id为空，那么就加载主分类下所有按照排序的方法
		if(otherTypeId==-1){
			tvs = tvService.selectTvByMainTypeId(mainTypeId, sortStr);
		}else{
			tvs = tvService.selectTvByOtherTypeId(otherTypeId, sortStr);
		}
		//分页
   	 	PageInfo<Tv> page = new PageInfo<>(tvs);
		return page;
	}
	
	public String typeSort(String sort){
		String sortStr = "";
		switch(sort){
			//创建日期
			case "date":sortStr="createdate";break;
			//点赞次数
			case "time":sortStr="praise_times";break;
			//评分
			case "grade":sortStr="grades";break;
			default:sortStr="createdate";break;
		}
		return sortStr;
			
	}

}
